﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace TimerTask.Common.Utils
{
    public class HttpUtil
    {
        /// <summary>
        /// http Get请求
        /// </summary>
        /// <param name="site"></param>
        /// <param name="path"></param>
        /// <param name="dicParams"></param>
        /// <returns></returns>
        public static async Task<string> DoGet(string site, string path, IDictionary<string, string> dicParams = null)
        {
            List<string> items = new List<string>();
            string urlParam = string.Empty;
            if (dicParams != null && dicParams.Count > 0)
            {
                foreach (var kv in dicParams)
                {
                    items.Add(string.Format("{0}={1}", kv.Key, kv.Value));
                }
                urlParam = string.Join("&", items);
            }
            string url = string.Empty;
            if (!string.IsNullOrEmpty(urlParam))
            {
                url = site + path + "?" + urlParam;
            }
            return await DoGet(url);
        }

        /// <summary>
        /// http Get请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async Task<string> DoGet(string url)
        {
            using (var client = new HttpClient())
            {
                try
                {
                    Uri uri = new Uri(url);
                    string result = await client.GetStringAsync(uri);
                    return result;
                }
                catch (Exception e)
                {
                    LogUtil.Error(e, "请求Get{0}出错：{1}", url);
                    return null;
                }
            }
        }


        /// <summary>
        /// http Post Json 请求
        /// </summary>
        /// <param name="site"></param>
        /// <param name="url"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static async Task<string> DoPostByJson(string site, string url, object args = null)
        {
            string postData = string.Empty;

            if (args != null)
            {
                postData = args.ToJson();
            }
            using (var client = new HttpClient())
            {
                try
                {
                    HttpContent hc = new StringContent(postData, Encoding.UTF8, "application/json");
                    Uri uri = new Uri(string.Format("{0}{1}", site, url));
                    HttpResponseMessage response = await client.PostAsync(uri, hc);
                    string responseBody = await response.Content.ReadAsStringAsync();
                    return responseBody;
                }
                catch (Exception e)
                {
                    LogUtil.Error(e, "请求Post{0}出错：{1}", url, args.ToJson());
                    return null;
                }
            }
        }

        /// <summary>
        /// http Post Json 请求
        /// </summary>
        /// <param name="site"></param>
        /// <param name="url"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static async Task<string> DoPostByParams(string site, string url, IDictionary<string, string> args = null)
        {
            string postData = string.Empty;

            List<string> items = new List<string>();
            if (args != null && args.Count > 0)
            {
                foreach (var kv in args)
                {
                    items.Add(string.Format("{0}={1}", kv.Key, kv.Value));
                }
                postData = string.Join("&", items);
            }
            using (var client = new HttpClient())
            {
                try
                {
                    HttpContent hc = new StringContent(postData, Encoding.UTF8, null);
                    Uri uri = new Uri(string.Format("{0}{1}", site, url));
                    HttpResponseMessage response = await client.PostAsync(uri, hc);
                    string responseBody = await response.Content.ReadAsStringAsync();
                    return responseBody;
                }
                catch (Exception e)
                {
                    LogUtil.Error(e, "请求Post{0}出错：{1}", url, args.ToJson());
                    return null;
                }
            }
        }
    }
}